package com.gdcn.report.buider;

import java.sql.Connection;
import java.util.List;

import com.gdcn.report.data.FileData;
import com.gdcn.report.service.ReportDataService;

public class SingleXMLBuilder extends Builder {
	
	String caption;

	List<String> data_x;
	List<List<String>> data_y;
	
	String xAxisName;
	String yAxisName;
	
	StringBuffer chartXML = new StringBuffer();

	public List<String> getData_x() {
		return data_x;
	}

	public void setData_x(List<String> data_x) {
		this.data_x = data_x;
	}

	public List<List<String>> getData_y() {
		return data_y;
	}

	public void setData_y(List<List<String>> data_y) {
		this.data_y = data_y;
	}

	@Override
	protected void buildBody() {
		int size = data_x.size();
		for(int i=0; i<size; i++) {
			chartXML.append("<set name='");
			chartXML.append(data_x.get(i));
			chartXML.append("' value='");
			chartXML.append(data_y.get(0).get(i));
			chartXML.append("' />");
		}
	}

	@Override
	protected void buildHeader() {
		chartXML.append("<graph caption='").append(caption).append("' xAxisName='").append(xAxisName).append("' yAxisName='").append(yAxisName).append("' >");
	}

	@Override
	protected String buildTail() {
		chartXML.append("</graph>");
		return chartXML.toString();
	}
	
	public String getChartXML() {
		return chartXML.toString();
	}

	public String getXAxisName() {
		return xAxisName;
	}

	public void setXAxisName(String axisName) {
		xAxisName = axisName;
	}

	public String getYAxisName() {
		return yAxisName;
	}

	public void setYAxisName(String axisName) {
		yAxisName = axisName;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	@Override
	public void init(ReportDataService reportService, Connection connection, FileData fd) {
		this.setData_x(reportService.query_x(connection, fd));
		this.setData_y(reportService.query_y(connection, fd));
		this.setCaption(fd.getCaption());
		this.setXAxisName(fd.getXAxisName());
		this.setYAxisName(fd.getYAxisName());
	}
}
